package com.web.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.web.dao.SysBlogDao;
import com.web.entity.SysBlog;
import com.web.service.SysBlogService;

@Service
public class SysBlogServiceImpl implements SysBlogService {

	@Autowired
	private SysBlogDao sysBlogDao;

	@Override
	public PageInfo selectShowBlogs(SysBlog sb, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize); // 调用分页功能
		List<SysBlog> list = sysBlogDao.getShowBlog(sb); // 上面的分页会自动注入到本条查询中
		System.out.println(JSONObject.toJSON(list));
		PageInfo<SysBlog> pageInfo = new PageInfo<SysBlog>(list); // 获取分页信息
		System.out.println(JSONObject.toJSON(pageInfo));
		return pageInfo;
	}

	@Override
	public List<SysBlog> getBlogs() {
		// TODO Auto-generated method stub
		return sysBlogDao.getBlog();
	}

	@Override
	public List<SysBlog> getBlogId(Integer id) {
		// TODO Auto-generated method stub
		return sysBlogDao.getBlogId(id);
	}

	@Override
	public Map<String, Object> changeBlogState(Integer id, Integer state) {
		Map<String, Object> map = new HashMap<String, Object>();
		sysBlogDao.updateBlogState(id, state);
		map.put("result", 1);
		return map;
	}

	@Override
	public Map<String, Object> deleteBlog(Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		sysBlogDao.delBlog(id);
		map.put("result", 1);
		return map;
	}

	@Override
	public Map<String, Object> addBlog(SysBlog sysBlog) {
		Map<String, Object> map = new HashMap<String, Object>();
		sysBlogDao.addBlog(sysBlog);
		map.put("result", 1);
		return map;
	}

	@Override
	public Map<String, Object> getBlogList(SysBlog sysBlog, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize); // 调用分页功能
		List<SysBlog> list = sysBlogDao.getBloglist(sysBlog); // 上面的分页会自动注入到本条查询中
		System.out.println(JSONObject.toJSON(list));
		PageInfo<SysBlog> pageInfo = new PageInfo<SysBlog>(list); // 获取分页信息
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("total", pageInfo.getTotal());
		resultMap.put("rows", pageInfo.getList());
		resultMap.put("navigatepageNums", pageInfo.getNavigatepageNums());
		return resultMap;
	}

	// 修改博客内容
	@Override
	public Map<String, Object> editorBlog(SysBlog sysBlog) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		sysBlogDao.editorBlog(sysBlog);
		resultMap.put("result", 1);
		return resultMap;
	}

}
